package com.example.sqlitedemo;

import android.Manifest;
import android.os.Debug;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DebugUtils;
import android.view.View;
import android.widget.Button;

import com.example.sqlitedemo.dao.entity.FaceFeature;
import com.example.sqlitedemo.dao.entity.ShootPhoto;
import com.example.sqlitedemo.dao.util.FaceDaoUtil;
import com.example.sqlitedemo.dao.util.PhotoDaoUtil;
import com.example.sqlitedemo.util.DebugLog;
import com.yanzhenjie.permission.AndPermission;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

public class MainActivity extends AppCompatActivity {

    private Button btnCreat;
    private static SimpleDateFormat sdf = new SimpleDateFormat("HHmmssSSS", Locale.CHINA);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnCreat = findViewById(R.id.btn_creat);
        requestPermission();

        btnCreat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestPermission();
            }
        });
    }

    /**
     * 模拟数据
     */
    private void initaData() {
//        List<FaceFeature> faceFeatures = new ArrayList<>();
        /**
         * 照片信息
         */
        ShootPhoto shootPhoto = new ShootPhoto();
        shootPhoto.setPhotoMd5(UUID.randomUUID().toString());
        shootPhoto.setFaceCount(3);
        shootPhoto.setPhotoName("DSCI0001");
        shootPhoto.setPhotoPath("sdcard/camara/00001.png");
        PhotoDaoUtil.getPhotoDao().insert(shootPhoto);
        /**
         * 照片信息
         */
        ShootPhoto shootPhoto2 = new ShootPhoto();
        shootPhoto2.setPhotoMd5(UUID.randomUUID().toString());
        shootPhoto2.setFaceCount(1);
        shootPhoto2.setPhotoName("DSCI0002");
        shootPhoto2.setPhotoPath("sdcard/camara/00002.png");
        PhotoDaoUtil.getPhotoDao().insert(shootPhoto2);
        /**
         * 人脸信息 1
         */

        for (int i = 0; i < 3; i++) {
            FaceFeature faceFeature = new FaceFeature();
            faceFeature.setUuid(UUID.randomUUID().toString());
            //制作一个同一个人脸
            if (i == 1) {
                faceFeature.setFaceTonken("kun123456");
            } else {

                faceFeature.setFaceTonken(sdf.format(new Date()));
                DebugLog.d(DebugLog.LOG + " 人脸tonken----- " + sdf.format(new Date()));
            }
            faceFeature.setFaceMd5(shootPhoto.getPhotoMd5());
            FaceDaoUtil.getFaceDao().insertOrReplace(faceFeature);
        }
        /**
         * 人脸信息 2
         */
        FaceFeature faceFeature = new FaceFeature();
        faceFeature.setUuid(UUID.randomUUID().toString());
        //同一个人脸，在第一张图片里，
        faceFeature.setFaceTonken("kun123456");
        faceFeature.setFaceMd5(shootPhoto2.getPhotoMd5());
        FaceDaoUtil.getFaceDao().insertOrReplace(faceFeature);

        DebugLog.d(DebugLog.LOG + "-----------------------------------------查询通过照片得到人脸的数据 -----------------------------------------------------");
        PhotoDaoUtil.getFaceList();
        DebugLog.d(DebugLog.LOG + "-----------------------------------------查询kun123456下的照片 -----------------------------------------------------");
        FaceDaoUtil.getMd5Pic("kun123456");
    }

    /**
     * 请求权限
     */
    private void requestPermission() {
        AndPermission.with(this)
                .permission(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                // 准备方法，和 okhttp 的拦截器一样，在请求权限之前先运行改方法，已经拥有权限不会触发该方法
                .rationale((context, permissions, executor) -> {
                    // 此处可以选择显示提示弹窗
                    executor.execute();
                })
                // 用户给权限了
                .onGranted(permissions ->
//                        ToastUtil.toastShortContent(MainActivity.this,getString(R.string.give))
                                initaData()
                )
                // 用户拒绝权限，包括不再显示权限弹窗也在此列
                .onDenied(permissions -> {
                    // 判断用户是不是不再显示权限弹窗了，若不再显示的话进入权限设置页
                    if (AndPermission.hasAlwaysDeniedPermission(MainActivity.this, permissions)) {
                        // 打开权限设置页
                        AndPermission.permissionSetting(MainActivity.this).execute();
                    }
                }).start();
    }

}
